home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DIALOGS
/
JANUSW
/
DEBUG.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1994-11-14
|
7KB
|
279 lines
{$A+,B-,G+,I-,O-,P+,Q-,R-,S-,T-,V-,X+}
Unit Debug;
{ Unit: Debug
Version: 1.00
Purpose: useful functions for debug output
Uses: DbWin or monochrome monitor as output device
Date: 09/20/94
Developer: Peter Sawatzki (ps)
Buchenhof 3, 58091 Hagen, Germany
CompuServe: 100031,3002
Contributing: Jeroen W. Pluimers (jwp), CIS: 100013,1443
Date: Author:
08/01/93 ps wrote it
01/18/94 ps/jwp correct bug in debugoutput, add R- option
01/21/94 ps minor 'optimizations'
09/20/94 ps/jwp add HexP, LogFile and DumpXXX stuff from Jeroen's version
09/21/94 ps add DateTimeStr function
Copyright (c) 1994 Peter Sawatzki. All Rights Reserved.
}
Interface
Uses
WinTypes,
WinProcs,
WinDos,
Strings;
Type
Str2 = String[2];
Str4 = String[4];
Str8 = String[8];
Str9 = String[9];
Str10 = String[10];
PtrRec = Record
Ofs, Seg: Word
End;
LongRec = Record
LoWord, HiWord: Word
End;
pDataSeg = ^tDataSeg;
tDataSeg = Record
pNull: LongInt; { 00 - always 0 }
pPtrCount: Word; { 04 - number of pointers in structure }
pLocalHeap: Word; { 06 - near ptr to local heap }
pAtomTable: Word; { 08 - near ptr to atom table within local heap }
pStackTop: Word; { 0A - near ptr to top of the stack - SP can't go beyond this value }
pStackBot: Word; { 0C - near ptr to bottom of stack - initial value of SP }
pStackMin: Word; { 0E - near ptr to lowest stack value used - lowest value of SP }
End;
Procedure BreakPoint; Inline($CC);
Function HexB (b: Byte): Str2;
Function HexW (w: Word): Str4;
Function HexL (l: LongInt): Str8;
Function HexP (aPtr: Pointer): Str9;
Function L2S (l: LongInt): Str10;
Function W2S (w: Word): Str10;
Function StrPasEx(Str: pChar): String;
Function DateTimeStr: String;
Procedure AssignDebug (Var F: Text);
Implementation
Uses
{$IfDef Ver70} Win31, {$EndIf}
ToolHelp;
Const
HC: Array[0..$F] Of Char = '0123456789ABCDEF';
Function HexB (b: Byte): Str2;
Begin
HexB[0]:= #2;
HexB[1]:= HC[b Shr 4];
HexB[2]:= HC[b And $F]
End;
Function HexW (w: Word): Str4;
Begin
HexW[0]:= #4;
HexW[1]:= HC[w Shr 12];
HexW[2]:= HC[Hi(w) And $F];
HexW[3]:= HC[Lo(w) Shr 4];
HexW[4]:= HC[w And $F]
End;
Function HexL (l: LongInt): Str8;
Begin With LongRec(l) Do Begin
HexL[0]:= #8;
HexL[1]:= HC[HiWord Shr 12];
HexL[2]:= HC[Hi(HiWord) And $F];
HexL[3]:= HC[Lo(HiWord) Shr 4];
HexL[4]:= HC[HiWord And $F];
HexL[5]:= HC[LoWord Shr 12];
HexL[6]:= HC[Hi(LoWord) And $F];
HexL[7]:= HC[Lo(LoWord) Shr 4];
HexL[8]:= HC[LoWord And $F]
End End;
Function HexP (aPtr: Pointer): Str9;
Begin With LongRec(aPtr) Do Begin
HexP[0]:= #9;
HexP[1]:= HC[HiWord Shr 12];
HexP[2]:= HC[Hi(HiWord) And $F];
HexP[3]:= HC[Lo(HiWord) Shr 4];
HexP[4]:= HC[HiWord And $F];
HexP[5]:= ':';
HexP[6]:= HC[LoWord Shr 12];
HexP[7]:= HC[Hi(LoWord) And $F];
HexP[8]:= HC[Lo(LoWord) Shr 4];
HexP[9]:= HC[LoWord And $F]
End End;
Function L2S (l: LongInt): Str10;
Var
pStr: ^Str10;
Begin
Asm Les Di, @Result; Mov Word(pStr), Di; Mov Word(pStr+2), Es End;
Str(l,pStr^)
End;
Function W2S (w: Word): Str10;
Var
pStr: ^Str10;
Begin
Asm Les Di, @Result; Mov Word(pStr), Di; Mov Word(pStr+2), Es End;
Str(w,pStr^)
End;
Function StrPasEx(Str: pChar): String;
Begin
If PtrRec(Str).Seg=0 Then
StrPasEx:= '#'+L2S(Word(Str))
Else
StrPasEx:= StrPas(Str)
End;
Function DateTimeStr: String;
Var
y,m,d,dummy, h, min, s: Word;
Begin
GetDate(y,m,d,dummy);
GetTime(h,min,s,dummy);
DateTimeStr:= L2S(y)+'/'+L2S(m)+'/'+L2S(d)+', '
+ L2S(h)+':'+L2S(min)+':'+L2S(s)
End;
{------------------------------------------ Debug output functions }
Procedure SpitOut (aStr: pChar);
Const
OutputTo: (oDontKnow, oDbWin, oFile, oIgnore) = oDontKnow;
DebugFile = 'C:\DEBUG.LOG';
Procedure SpitOutToFile (aStr: pChar);
Var
aTextFile: Text;
IoRes: Integer;
Begin
IoRes:= InOutRes; InOutRes:= 0;
Assign(aTextFile, DebugFile); Append(aTextFile);
If IoResult=0 Then Write(aTextFile, aStr);
If IoResult=0 Then Close(aTextFile);
InOutRes:= IoRes;
End;
Procedure CheckOutput;
Var
ModuleEntry: tModuleEntry;
Tmp: Array[0..30] Of Char;
aTextFile: Text;
IoRes: Integer;
Begin
ModuleEntry.dwSize:= SizeOf(tModuleEntry);
If (GetSystemMetrics(sm_debug)=0)
And (ModuleFindName(@ModuleEntry, 'DBWIN')=0) Then Begin
OutputTo:= oFile;
IoRes:= InOutRes; InOutRes:= 0;
Assign(aTextFile, DebugFile); Append(aTextFile);
If IoResult<>0 Then ReWrite(aTextFile);
If IoResult<>0 Then OutputTo:= oIgnore;
InOutRes:= IoRes
End Else
OutputTo:= oDbWin;
If OutputTo<>oIgnore Then Begin
SpitOut('---- Log startet on ');
SpitOut(StrPCopy(Tmp, DateTimeStr));
SpitOut(' ----'#13#10);
SpitOut(aStr)
End
End;
Begin {$i-}
Case OutputTo Of
oDbWin: OutputDebugString(aStr);
oFile: SpitOutToFile(aStr);
oDontKnow: CheckOutput;
End
End;
Function DebugOutput (Var F: tTextRec): Integer; Far;
Var
TwoCh: Array[0..1] Of Char;
Begin
With F Do If BufPos>0 Then Begin
TwoCh[0]:= #0; TwoCh[1]:= #0;
If BufPos=BufSize Then Begin
Dec(BufPos);
TwoCh[0]:= BufPtr^[BufPos]
End;
BufPtr^[BufPos]:= #0;
SpitOut(pChar(BufPtr));
If TwoCh[0]<>#0 Then
SpitOut(TwoCh);
BufPos:= 0
End;
DebugOutput:= 0
End;
Function DebugClose (Var F: tTextRec): Integer; Far;
Begin
DebugClose:= 0
End;
Function DebugOpen (Var F: tTextRec): Integer; Far;
Begin With F Do Begin
Mode:= fmOutput;
InOutFunc:= @DebugOutput;
FlushFunc:= @DebugOutput;
CloseFunc:= @DebugClose;
DebugOpen:= 0
End End;
Procedure AssignDebug (Var F: Text);
Begin With tTextRec(F) Do Begin
Handle:= $FFFF;
Mode:= fmClosed;
BufSize:= SizeOf(Buffer);
BufPtr:= @Buffer;
OpenFunc:= @DebugOpen;
Name[0]:= #0
End End;
Procedure DumpDefData;
Var
DataSeg: pDataSeg;
Begin
DataSeg := Ptr(DSeg, 0);
WriteLn('t DumpDefData - data segment: ',HexP(DataSeg));
With DataSeg^ Do Begin
WriteLn('t 00 pNull: ',HexL(pNull));
WriteLn('t 04 pPtrCount: ',HexW(pPtrCount));
WriteLn('t 06 pLocalHeap: ',HexW(pLocalHeap));
WriteLn('t 08 pAtomTable: ',HexW(pAtomTable));
WriteLn('t 0A pStackTop: ',HexW(pStackTop));
WriteLn('t 0C pStackBot: ',HexW(pStackBot));
WriteLn('t 0E pStackMin: ',HexW(pStackMin));
WriteLn('t used stack: ',Longint(pStackBot)-pStackMin,' (bytes)');
WriteLn('t stack size: ',Longint(pStackBot)-pStackTop,' (bytes)');
End
End;
Procedure DumpResourceInfo;
Begin
WriteLn('t DumpResourceInfo - free resources');
WriteLn('System heap: ',GetFreeSystemResources(GFSR_SystemResources),' %');
WriteLn('GDI heap: ',GetFreeSystemResources(GFSR_GDIResources),' %');
WriteLn('User heap: ',GetFreeSystemResources(GFSR_UserResources),' %');
WriteLn('MemAvail: ',MemAvail:8);
WriteLn('MaxAvail: ',MaxAvail:8);
End;
Begin
AssignDebug(Output);
Rewrite(Output)
End.